﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Pixysoft.Framework.Reflection.Controller
{
    static class ReflectionFactory
    {
        private const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

        public static MethodInfo GetMethodInfo<Class, Attribute>()
        {
            Type type = typeof(Class);

            foreach (MethodInfo method in type.GetMethods(flags))
            {
                if (method.GetCustomAttributes(typeof(Attribute), true).Length > 0)
                    return method;
            }

            return null;
        }

        public static PropertyInfo GetPropertyInfo<Class, Attribute>()
        {
            Type type = typeof(Class);

            foreach (PropertyInfo info in type.GetProperties(flags))
            {
                if (info.GetCustomAttributes(typeof(Attribute), true).Length > 0)
                    return info;
            }

            return null;
        }

        public static bool IsInheritFrom(Type objectType, Type baseType)
        {
            if (objectType.BaseType == null)
                return false;

            if (objectType.BaseType.Equals(typeof(object)))
                return false;

            if (objectType.BaseType.Equals(baseType))
                return true;

            return IsInheritFrom(objectType.BaseType, baseType);
        }
    }
}
